<template>
  <div class="paho-map">paho</div>
</template>
<script>
import Paho from "./paho-mqtt.js";

export default {
  data() {
    return {
      client: new Paho.Client("82.157.27.251", 61614, "js-utility-" + makeid()),
      topic: "topic"
    };
  },
  methods: {
    makeid() {
      var text = "";
      var possible =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

      for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

      return text;
    },

    onConnectedLost(responseObject) {
      console.log("onConnectionLost:" + responseObject.errorMessage);
    },
    onMessageArrived(message) {
      console.log("onMessageArrived:" + message.payloadString);
      let log = JSON.parse(message.payloadString);
      console.log(log, "log");
      console.log(log.expandData.longitude);
    },
    onConnect() {
      // Once a connection has been made, make a subscription and send a message.
      console.log("onConnect");
      this.client.subscribe(this.topic);
      // message = new Paho.MQTT.Message("Hello");
      // message.destinationName = "World";
      // client.send(message);
    }
  },
  created() {
    this.client.connect({
      onSuccess: this.onConnect
    });
    this.client.onConnectedLost = this.onConnectedLost;
    this.client.onMessageArrived = this.onMessageArrived;
  }
};
</script>
